﻿/* **********************************************************************************
 *
 * Copyright (c) WickedFlame Projects. All rights reserved.
 *
 * This source code is subject to terms and conditions of the Microsoft Reciprocal License
 * for WickedFlame. A copy of the license can be found in the License.html file
 * at the root of this distribution. 
 * By using this source code in any fashion, you are agreeing to be bound by
 * the terms of the Microsoft Reciprocal License for WickedFlame.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * **********************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Resources;

namespace Gencode.Controls.CollapsibleControl
{
	[ToolboxItem(false)]
	public partial class CollapsibleItemHeader : UserControl
	{
        string m_text = string.Empty;
        Font m_font = new Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        
        bool isCollapsed = false;
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool IsCollapsed
        {
            get
            {
                return isCollapsed;
            }
            set
            {
                isCollapsed = value;
                pictureBox1_Click(null, new EventArgs());
            }
        }

        ResourceManager rm = null;

		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public event ListHeaderStateChangedEventHandler ListHeaderStateChanged;

		public CollapsibleItemHeader()
		{
			InitializeComponent();

            rm = new ResourceManager(this.GetType());
		}

        public CollapsibleItemHeader(string text)
        {
            this.m_text = text;
            InitializeComponent();
        }

        // The text to display inside the control.
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override string Text
        {
            get
            {
                return (m_text);
            }
            set
            {
                m_text = value;
            }
        }

        // Font for text to display inside the control.
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override Font Font
        {
            get
            {
                return m_font;
            }
            set
            {
                m_font = value;
            }
        }

        private void CollapsibleItemHeader_Paint(object sender, PaintEventArgs e)
        {
            LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, this.Width, this.Height), Color.FromArgb(219, 217, 200), Color.FromArgb(196, 193, 176), 90);
            e.Graphics.FillRectangle(brush, new Rectangle(0, 0, this.Width, this.Height));

            // Render the text next to the plus/minus box.
            int textOffsetX = pictureBox1.Location.X + pictureBox1.Width;
            RectangleF textRect = new RectangleF(textOffsetX + 4, 0, this.Width - textOffsetX, this.Height);
            StringFormat textFormat = new StringFormat();
            textFormat.Alignment = StringAlignment.Near;
            textFormat.LineAlignment = StringAlignment.Center;
            e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), textRect, textFormat);
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Image plusImage;

            if (isCollapsed)
            {
                plusImage = (Image)Gencode.Controls.Properties.Resources.plusbox;
                isCollapsed = false;
                OnListHeaderStateChanged(new ListHeaderStateChangedEventArgs(ListHeaderState.Collapsed));
            }
            else
            {
                plusImage = (Image)Gencode.Controls.Properties.Resources.minus;
                isCollapsed = true;
                OnListHeaderStateChanged(new ListHeaderStateChangedEventArgs(ListHeaderState.Expanded));
            }

            pictureBox1.Image = plusImage;
        }

        protected virtual void OnListHeaderStateChanged(ListHeaderStateChangedEventArgs e)
        {
            if (ListHeaderStateChanged != null) ListHeaderStateChanged(this, e);
        }

        internal void OnResize(object sender, CollapsibelControlResizeEventArgs e)
        {
			this.Width = e.ParentClientWidth - (this.Margin.Left + this.Margin.Right);
        }
	}

    public class ListHeaderStateChangedEventArgs : EventArgs
    {
        private ListHeaderState _state;

        public ListHeaderStateChangedEventArgs(ListHeaderState state)
            : base()
        {
            _state = state;
        }

        public ListHeaderState State
        {
            get
            {
                return (_state);
            }
        }
    }
    public enum ListHeaderState
    {
        Expanded = 0,
        Collapsed
    }

    public delegate void ListHeaderStateChangedEventHandler(object sender, ListHeaderStateChangedEventArgs e);
}
